import { otherHttp } from '/@/utils/http/axios';
import { ErrorMessageMode } from '/#/axios';
import {
  addAttributesParams,
  addCategoryParams,
  addProductParams,
  deleteAttributesParams,
  getCategoriesParams,
  getCategoryAttributesParams,
  getProductParams,
  getSingleAttributeParams,
  updateAttributesParams,
  updateCategoryParams,
  updateProductParams,
} from './model/shopGoodsModel';

enum Api {
  goods = '/goods',
  categories = '/categories',
  upload = '/upload',
}

/**
 * @description: shop product list api
 */
export function shopProductApi(params: getProductParams, mode: ErrorMessageMode = 'message') {
  return otherHttp.get<Array<Object>>(
    {
      url: Api.goods,
      params,
    },
    {
      errorMessageMode: mode,
    },
  );
}
/**
 * @description: add shop product api
 */
export function addShopProductApi(params: addProductParams, mode: ErrorMessageMode = 'message') {
  return otherHttp.post<Object>(
    {
      url: Api.goods,
      params,
    },
    {
      errorMessageMode: mode,
    },
  );
}
/**
 * @description: get categories api
 */
export function getCategoriesApi(params: getCategoriesParams, mode: ErrorMessageMode = 'message') {
  return otherHttp.get<object[]>(
    {
      url: Api.categories,
      params,
    },
    {
      errorMessageMode: mode,
    },
  );
}
/**
 * @description: add category api
 */
export function addCategoryApi(params: addCategoryParams, mode: ErrorMessageMode = 'message') {
  return otherHttp.post<object>(
    {
      url: Api.categories,
      params,
    },
    {
      errorMessageMode: mode,
    },
  );
}
/**
 * @description: delete category api
 */
export function deleteCategoryApi(params: { id: number }, mode: ErrorMessageMode = 'message') {
  const { id } = params;
  return otherHttp.delete<void>(
    {
      url: Api.categories + `/${id}`,
    },
    {
      errorMessageMode: mode,
    },
  );
}
/**
 * @description: update category api
 */
export function updateCategoryApi(
  params: updateCategoryParams,
  mode: ErrorMessageMode = 'message',
) {
  const { id, cat_name } = params;
  return otherHttp.put<void>(
    {
      url: Api.categories + `/${id}`,
      params: {
        cat_name,
      },
    },
    {
      errorMessageMode: mode,
    },
  );
}
/**
 * @description: get category attributes api
 */
export function getCategoryAttributesApi(
  params: getCategoryAttributesParams,
  mode: ErrorMessageMode = 'message',
) {
  const { id, sel } = params;
  return otherHttp.get<object[]>(
    {
      url: Api.categories + `/${id}/attributes`,
      params: {
        sel,
      },
    },
    {
      errorMessageMode: mode,
    },
  );
}
/**
 * @description: upload image api
 */
export function uploadApi(params: FormData, mode: ErrorMessageMode = 'message', callback: Fn) {
  return otherHttp.post<Object>(
    {
      headers: {
        'Content-Type': 'multipart/form-data',
      },
      url: Api.upload,
      params,
      onUploadProgress: ({ total, loaded }) => {
        callback({ percent: Math.round((loaded / total) * 100) });
      },
    },
    {
      errorMessageMode: mode,
    },
  );
}
/**
 * @description: get single product api
 */
export function getSingleProductApi(params: { id: number }, mode: ErrorMessageMode = 'message') {
  const { id } = params;
  return otherHttp.get<Object | any>(
    {
      url: Api.goods + `/${id}`,
    },
    {
      errorMessageMode: mode,
    },
  );
}
/**
 * @description: update single product api
 */
export function updateSingleProductApi(
  params: updateProductParams,
  mode: ErrorMessageMode = 'message',
) {
  const { id } = params;
  return otherHttp.put<Object | any>(
    {
      url: Api.goods + `/${id}`,
      params,
    },
    {
      errorMessageMode: mode,
    },
  );
}
/**
 * @description: delete single product api
 */
export function deleteSingleProductApi(params: { id: number }, mode: ErrorMessageMode = 'message') {
  const { id } = params;
  return otherHttp.delete<Object | any>(
    {
      url: Api.goods + `/${id}`,
    },
    {
      errorMessageMode: mode,
    },
  );
}
/**
 * @description: add attributes api
 */
export function addAttributesApi(params: addAttributesParams, mode: ErrorMessageMode = 'message') {
  const { id, attr_name, attr_sel, attr_vals } = params;
  return otherHttp.post<Object | any>(
    {
      url: Api.categories + `/${id}/attributes`,
      params: {
        attr_name,
        attr_sel,
        attr_vals,
      },
    },
    {
      errorMessageMode: mode,
    },
  );
}
/**
 * @description: update attributes api
 */
export function updateAttributesApi(
  params: updateAttributesParams,
  mode: ErrorMessageMode = 'message',
) {
  const { id, attr_id, attr_name, attr_sel, attr_vals } = params;
  return otherHttp.put<Object | any>(
    {
      url: Api.categories + `/${id}/attributes/${attr_id}`,
      params: {
        attr_name,
        attr_sel,
        attr_vals,
      },
    },
    {
      errorMessageMode: mode,
    },
  );
}
/**
 * @description: get single attribute api
 */
export function getSingleAttributeApi(
  params: getSingleAttributeParams,
  mode: ErrorMessageMode = 'message',
) {
  const { id, attr_id, attr_sel, attr_vals } = params;
  return otherHttp.get<Object | any>(
    {
      url: Api.categories + `/${id}/attributes/${attr_id}`,
      params: {
        attr_sel,
        attr_vals,
      },
    },
    {
      errorMessageMode: mode,
    },
  );
}
/**
 * @description: delete attributes api
 */
export function deleteAttributesApi(
  params: deleteAttributesParams,
  mode: ErrorMessageMode = 'message',
) {
  const { id, attrid } = params;
  return otherHttp.delete<Object | any>(
    {
      url: Api.categories + `/${id}/attributes/${attrid}`,
    },
    {
      errorMessageMode: mode,
    },
  );
}
